package controllers

import java.io.File
import play.api.mvc._
import tool.{FormTool, Tool}
import utils.Utils

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global

/**
  * Created by yz on 2017/6/7.
  */
class DownloadController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {


  def toIndex = Action {
    Ok(views.html.download.index())
  }

  def download = Action { implicit request =>
    val data=FormTool.fileNameForm.bindFromRequest().get
    val file = new File(Tool.downloadDir, data.fileName)
    Ok.sendFile(file).withHeaders(
      CACHE_CONTROL -> "max-age=3600",
      CONTENT_DISPOSITION -> ("attachment; filename=" + file.getName),
      CONTENT_TYPE -> "application/x-download"
    )
  }

}
